<?php

class EmpresasController extends Controller
{
	public $layout='//layouts/templete';

	public function actionIndex()
	{

		$tipo=Yii::app()->user->getState('id_tipo');
		$id_usuario=Yii::app()->user->getState('id_usuario');
		if($tipo==='ADMIN' && $id_usuario>0)
		{
			$total=empresas::model()->count();
			$pages=new CPagination($total);
			$pages->pageSize=10;
			$criteria=new CDbCriteria();

			$criteria->limit=$pages->limit;
			$criteria->offset=$pages->offset;

		 	$model=empresas::model()->findAll($criteria);
			$this->render('index', array('model' =>$model,'pages'=>$pages ));	
		}
		else
		{
			$this->redirect(array('site/index'));
		}
		
	}
	public function actionNuevo(){
		$tipo=Yii::app()->user->getState('id_tipo');
		$id_usuario=Yii::app()->user->getState('id_usuario');
		if($tipo==='ADMIN' && $id_usuario>0)
		{
				
         $model=new empresas;

 		if(isset($_POST['Empresas']))
    		{
       		 $model->attributes=$_POST['Empresas'];
       		 if($model->validate())
       			 {
            // form inputs are valid, do something here
            	if($model->save())
            	{
            		$this->redirect(array('index'));
       			 }
    			}
			 //$this->render('formEmpresas',array('model'=>$model));	
		}
		
		$this->render('formEmpresas',array('model'=>$model));	
	}
	else
		{
			$this->redirect(array('site/index'));
		}
}

public function actionUpdate($id)
{
	$tipo=Yii::app()->user->getState('id_tipo');
		$id_usuario=Yii::app()->user->getState('id_usuario');
		if($tipo==='ADMIN' && $id_usuario>0)
		{
			$model=empresas::model()->findByPk($id);
			if(isset($_POST['Empresas']))
			{
				$model->attributes=$_POST['Empresas'];
				if($model->save())
				{
				$this->redirect(array('index'));
				}
			}
		$this->render('formEmpresas',array('model'=>$model));	
		}
		else
		{
			$this->redirect(array('site/index'));
		}

}
public function actionDelete($id)
{
		$tipo=Yii::app()->user->getState('id_tipo');
		$id_usuario=Yii::app()->user->getState('id_usuario');
		if($tipo==='ADMIN' && $id_usuario>0)
		{
			$model=empresas::model()->deleteByPk($id);
			$this->redirect(array('index'));
		}
		else
		{
			$this->redirect(array('site/index'));
		}
}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}